package com.a2.appa3.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.a2.appa3.R;
import com.a2.appa3.dagger.component.activity.DaggerRegisterActivityComponent;
import com.a2.appa3.dagger.module.activity.RegisterActivityModule;
import com.a2.appa3.model.network.entity.req.ResCode;
import com.a2.appa3.model.network.entity.req.Resregister;
import com.a2.appa3.presenter.activity.RegisterActivityPresenter;
import com.a2.appa3.util.Md5;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import me.shihao.library.XStatusBarHelper;

public class RegisterActivity extends BaseActivity<RegisterActivityPresenter> {


    @BindView(R.id.ivback)
    ImageView back;
    @BindView(R.id.headerTitle)
    TextView headerTitle;
    @BindView(R.id.headerButton)
    ImageView headerButton;
    @BindView(R.id.headerButtonRoot)
    LinearLayout llHeaderRoot;
    @BindView(R.id.logintitle)
    TextView logintitle;
    @BindView(R.id.user_name_et)
    EditText userNameEt;
    @BindView(R.id.onclockimg)
    ImageView onclockimg;
    @BindView(R.id.user_pass_et)
    EditText userPassEt;
    @BindView(R.id.yearpass)
    CheckBox yearpass;
    @BindView(R.id.onclockimg1)
    ImageView onclockimg1;
    @BindView(R.id.user_pass_et1)
    EditText userPassEt1;
    @BindView(R.id.yearpass1)
    CheckBox yearpass1;
    @BindView(R.id.realname_et)
    EditText realnameEt;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.phonelin)
    LinearLayout phonelin;
    @BindView(R.id.onclockimg0)
    ImageView onclockimg0;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.code_img)
    ImageView codeImg;
    @BindView(R.id.savepass)
    CheckBox savepass;
    @BindView(R.id.play_bt)
    TextView playBt;
    @BindView(R.id.loginlayout)
    LinearLayout loginlayout;
    @BindView(R.id.loginbt)
    TextView loginbt;
    @BindView(R.id.Scrowlogin)
    View Scrowlogin;
    private String peuser;
    private String username;
    private String password;
    private String password2;
    private String realname;
    private String phone;
    private String code;
    private String codes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XStatusBarHelper.tintStatusBar(this, getResources().getColor(R.color.settingbg));
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mPresenter.getCode();
        headerTitle.setText("注册");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

//        userNameEt.setOnFocusChangeListener(new android.view.View.
//                OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    Scrowlogin.setVisibility(View.VISIBLE);
//                } else {
//                    Scrowlogin.setVisibility(View.GONE);
//                }
//            }
//        });
        userNameEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });
        userPassEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });
        userPassEt1.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });
        realnameEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });
        phoneEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });
        codeEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });

        yearpass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = userPassEt.getText().length();
                userPassEt.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                userPassEt.setSelection(passwordLength);
            }
        });
        yearpass1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = userPassEt1.getText().length();
                userPassEt1.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                userPassEt1.setSelection(passwordLength);
            }
        });
    }

    @Override
    protected void initDagger() {
        DaggerRegisterActivityComponent.builder()
                .registerActivityModule(new RegisterActivityModule(this))
                .build().inject(this);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_register;
    }


    @OnClick({R.id.loginbt, R.id.code_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.loginbt:
//                peuser = ReferrerUserEt.getText().toString().trim();//推荐人
                username = userNameEt.getText().toString().trim();//账号
                password = userPassEt.getText().toString().trim();//密码
                password2 = userPassEt1.getText().toString().trim();//新密码
                realname = realnameEt.getText().toString().trim();//真实姓名
                phone = phoneEt.getText().toString().trim();//手机号
                code = codeEt.getText().toString().trim();//用户输入的验证码
//                if (TextUtils.isEmpty(peuser)) {
//                    Toast.makeText(RegisterActivity.this, "请输入推荐人", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(RegisterActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password2)) {
                    Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(realname)) {
                    Toast.makeText(RegisterActivity.this, "请输入真实姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!password.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{6,}$")) {
                    Toast.makeText(RegisterActivity.this, "您的密码不符合规定", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    Toast.makeText(RegisterActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!password.equals(password2)) {
                    Toast.makeText(RegisterActivity.this, "两次密码必须一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                if (TextUtils.isEmpty(password2)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!isMatcherFinded("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$", username)) {
                    Toast.makeText(RegisterActivity.this, "账号必须为6-12位字母和数字组成", Toast.LENGTH_LONG).show();
                    return;
                }
                String password1 = Md5.toMd5(password);
                mPresenter.register(username, password1, realname, phone, peuser, codes, code);
                break;
            case R.id.code_img:
                mPresenter.getCode();
                break;
        }
    }

    public void onCode(ResCode resCode) {
        switch (resCode.data.register_type) {
            case 0:
                phonelin.setVisibility(View.GONE);
                break;
            case 1:
                phonelin.setVisibility(View.VISIBLE);
                phoneEt.setHint("11位手机号");
                break;
            case 2:
                phonelin.setVisibility(View.VISIBLE);
                phoneEt.setHint("请输入qq号");
                break;
            case 3:
                phonelin.setVisibility(View.VISIBLE);
                phoneEt.setHint("请输入微信号");
                break;
            default:
                Toast.makeText(RegisterActivity.this, "验证码接口返回有误", Toast.LENGTH_SHORT).show();
                break;
        }
        codes = "";
        codes = resCode.data.captcha_code;
        Bitmap bitmap = null;
        byte[] bitmapArray;
        bitmapArray = Base64.decode(resCode.data.code, Base64.NO_WRAP);
        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        codeImg.setImageBitmap(bitmap);//显示图片

    }

    public void onRegister() {
        mPresenter.getCode();
    }

    public void onUsermessage(Resregister resregister) {
        //自动登录
        String password1 = Md5.toMd5(password);
        mPresenter.finduser(resregister.data.token);
    }

    public static boolean isMatcherFinded(String patternStr, CharSequence input) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            return true;
        }
        return false;
    }

    public void succucs() {
        finish();
        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();

    }

}
